package com.it.you.base.trytest;

import com.it.you.common.utils.Conv;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

/**
 * 拆解购物车PO
 * created by wujian on 2022/3/7 8:59
 */
@Data
public class SplitCartPO implements Serializable {
    private CartInfoPO cartInfoPO; //购物信息实体
    private String orderCode; //订单编号
    private Long storeId = 0L; //店铺id
    private Long storeType = 1L;//店铺类型 1-自营，4-三方
    private Integer nearItemFlag = 0; //临期商品 0-不参与拆单逻辑  1-临期  2-非临期
    private String storageCondition = ""; //存储条件 默认为""不参与拆单逻辑
    private Integer specialItemFlag = 0; //特药商品 0-不参与拆单 1-特药 2-非特药
    private Long postageRuleId = 0L; //运费策略id
    private Long merchantId = 0L; //商户id
    private String taxRate = ""; //税率
    private String businessType = ""; //业务类型
    private Integer dutyFlag = 0; //负责商品标识 0-不参与拆单逻辑 1-负责商品 2-非负责商品
    private Integer ecErp = 0;//此订单是否是走电商erp
    private Long itemPresellId = 0L;//预售商品批次id

    public SplitCartPO setCartInfo(CartInfoPO cartInfoPO) {
        this.cartInfoPO = cartInfoPO;
        return this;
    }

    //店铺
    public SplitCartPO addStore() {
        storeId = cartInfoPO.getStoreId();
        return this;
    }

    //商户
    public SplitCartPO addMerchant() {
        ItemBasicPO itemBasicPO = cartInfoPO.getItemBasicPO();
        if (null != itemBasicPO) {
            String supplierId = itemBasicPO.getSupplierId();
            if (!Conv.NS(supplierId).trim().equals("")) {
                //合营拆一单
                merchantId =NUM_ONE.longValue();
            }
        }
        return this;
    }

    public SplitCartPO addItemPreSell() {
        if (cartInfoPO.getItemBasicPO() != null && Conv.NL(cartInfoPO.getItemBasicPO().getItemPresellId()) != 0L) {
            itemPresellId = cartInfoPO.getItemBasicPO().getItemPresellId();
        }
        return this;
    }

    //临期
    public SplitCartPO addNear() {
//    cartInfoPO.getItemBasicPO()
        return this;
    }

    //存储条件
    public SplitCartPO addStorageCondition() {
        ItemBasicPO itemBasicPO = cartInfoPO.getItemBasicPO();
        if (null != itemBasicPO) {
            String storageConditionId = itemBasicPO.getStorageConditionId();
            if (StringUtils.isNotBlank(storageConditionId)) {
                if (storageConditionId.equals("L") || storageConditionId.equals("D")) {
                    //冷藏冷冻单独拆一单
                    storageCondition = "D";
                }
                //storageCondition = storageConditionId;
            }
        }
        return this;
    }

    //特药
    public SplitCartPO addSpecial() {
        ItemBasicPO itemBasicPO = cartInfoPO.getItemBasicPO();
        Integer specialFlag = null != itemBasicPO.getIsSpecialControl() && itemBasicPO.getIsSpecialControl() ? NUM_ONE : NUM_TWO;
        specialItemFlag = specialFlag;
        return this;
    }

//    //运费
//    public SplitCartPO addPostageRule() {
//        ItemFreightVO itemFreightVO = cartInfoPO.getItemFreightVO();
//        if (null != itemFreightVO) {
//            postageRuleId = itemFreightVO.getItemStoreFreightStrategyId();
//        }
//        return this;
//    }

    //税率
    public SplitCartPO addTaxRate() {
        ItemBasicPO itemBasicPO = cartInfoPO.getItemBasicPO();
        if (null != itemBasicPO) {
            String taxRateReq = itemBasicPO.getTaxRate();
            if (StringUtils.isNotBlank(taxRateReq)) {
                taxRate = taxRateReq;
            }
        }
        return this;
    }

    //业务类型
    public SplitCartPO addBusinessType() {
        ItemBasicPO itemBasicPO = cartInfoPO.getItemBasicPO();
        if (null != itemBasicPO) {
            String businessTypeReq = itemBasicPO.getBusinessType();
            if (StringUtils.isNotBlank(businessTypeReq)) {
                businessType = businessTypeReq;
            }
        }
        return this;
    }

    //负责标识
    public SplitCartPO addDutyFlag() {
        Boolean dutyBusiness = cartInfoPO.getDutyFlag();
        if (null != dutyBusiness) {
            dutyFlag = dutyBusiness ? NUM_ONE : NUM_TWO;
        }
        return this;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final SplitCartPO that = (SplitCartPO) o;
        return getGroupByKey(this).equals(getGroupByKey(that));
    }

    @Override
    public int hashCode() {
        return NUM_FOUR.equals(Conv.NI(storeType)) ? Objects.hash(this.storeId, this.itemPresellId):
                Objects.hash(this.storeId, this.merchantId, this.nearItemFlag, this.storageCondition, this.specialItemFlag,
                this.postageRuleId, this.taxRate, this.businessType, this.dutyFlag, this.itemPresellId);
    }

    private Integer NUM_ONE =1;
    private Integer NUM_TWO =2;
    private Integer NUM_THREE =3;
    private Integer NUM_FOUR =4;
    private String SPLIT_COMMA =",";
    public String getGroupByKey(SplitCartPO splitCartPO) {
        StringBuffer keyBuffer = new StringBuffer();
        if (NUM_FOUR.equals(Conv.NI(splitCartPO.getStoreType()))) {
            keyBuffer.append(splitCartPO.getStoreId()).append(SPLIT_COMMA)
                    .append(splitCartPO.getItemPresellId());
        } else {
            keyBuffer.append(splitCartPO.getStoreId()).append(SPLIT_COMMA)
                    .append(splitCartPO.getMerchantId()).append(SPLIT_COMMA)
                    .append(splitCartPO.getNearItemFlag()).append(SPLIT_COMMA)
                    .append(splitCartPO.getStorageCondition()).append(SPLIT_COMMA)
                    .append(splitCartPO.getSpecialItemFlag()).append(SPLIT_COMMA)
                    .append(splitCartPO.getTaxRate()).append(SPLIT_COMMA)
                    .append(splitCartPO.getBusinessType()).append(SPLIT_COMMA)
                    .append(splitCartPO.getDutyFlag()).append(SPLIT_COMMA)
                    .append(splitCartPO.getItemPresellId()).append(SPLIT_COMMA)
                    .append(splitCartPO.getPostageRuleId());
        }
        return keyBuffer.toString();
    }
}
